package com.adgox.tiantianbiting.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.ProgramBean;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2018/12/25.
 */

public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ViewHolder> implements View.OnClickListener {

    private List<ProgramBean> datas;

    public ProgramAdapter(List<ProgramBean> datas) {
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_program, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvTile.setText(datas.get(position).getTitle());
        holder.tv1.setText(datas.get(position).getSubProgram().get(0).getDescribe());
        holder.tv2.setText(datas.get(position).getSubProgram().get(1).getDescribe());
        holder.tv3.setText(datas.get(position).getSubProgram().get(2).getDescribe());
        Glide.with(holder.itemView.getContext()).load(datas.get(position).getSubProgram().get(0).getShareImageUrl()).into(holder.iv1);
        Glide.with(holder.itemView.getContext()).load(datas.get(position).getSubProgram().get(1).getShareImageUrl()).into(holder.iv2);
        Glide.with(holder.itemView.getContext()).load(datas.get(position).getSubProgram().get(2).getShareImageUrl()).into(holder.iv3);

        holder.tvMore.setTag(position);
        holder.tv1.setTag(position);
        holder.iv1.setTag(position);
        holder.tv2.setTag(position);
        holder.iv2.setTag(position);
        holder.tv3.setTag(position);
        holder.iv3.setTag(position);
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_tile)
        TextView tvTile;
        @BindView(R.id.tv_more)
        TextView tvMore;
        @BindView(R.id.iv_1)
        ImageView iv1;
        @BindView(R.id.iv_2)
        ImageView iv2;
        @BindView(R.id.iv_3)
        ImageView iv3;
        @BindView(R.id.tv_1)
        TextView tv1;
        @BindView(R.id.tv_2)
        TextView tv2;
        @BindView(R.id.tv_3)
        TextView tv3;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            tvMore.setOnClickListener(ProgramAdapter.this);
            iv1.setOnClickListener(ProgramAdapter.this);
            tv1.setOnClickListener(ProgramAdapter.this);
            iv2.setOnClickListener(ProgramAdapter.this);
            tv2.setOnClickListener(ProgramAdapter.this);
            iv3.setOnClickListener(ProgramAdapter.this);
            tv3.setOnClickListener(ProgramAdapter.this);
        }
    }

    public enum ViewType{
        MORE,
        PROGRAM1,
        PROGRAM2,
        PROGRAM3,
        ITEM
    }

    public interface OnItemClickListener{
        void onProgramItemClick(View view,ViewType viewType,int position);
    }

    private OnItemClickListener listener = null;

    public void setOnItemClick (OnItemClickListener listener ){
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        int postion = (int) view.getTag();
        if (listener != null){
            switch (view.getId()){
                case R.id.tv_more:
                    listener.onProgramItemClick(view,ViewType.MORE,postion);
                    break;
                case R.id.tv_1:
                case R.id.iv_1:
                    listener.onProgramItemClick(view,ViewType.PROGRAM1,postion);
                    break;
                case R.id.tv_2:
                case R.id.iv_2:
                    listener.onProgramItemClick(view,ViewType.PROGRAM2,postion);
                    break;
                case R.id.tv_3:
                case R.id.iv_3:
                    listener.onProgramItemClick(view,ViewType.PROGRAM3,postion);
                    break;
                default:
                    listener.onProgramItemClick(view,ViewType.ITEM,postion);
            }
        }
    }
}
